#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qtlogbrowser.h"

#include <QDebug>
#include <QDateTime>

QtLogBrowser *g_msglog_show = NULL;
void logMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QString strMessage = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
    QByteArray localMsg = msg.toLocal8Bit();
    const char *file = context.file ? context.file : "";
    const char *function = context.function ? context.function : "";
    Q_UNUSED(file)
    Q_UNUSED(function)

    strMessage += (QtWarningMsg == type) ?  " [ERROR] " : " [INFO] ";
    strMessage += msg;
    if (NULL != g_msglog_show) g_msglog_show->appendPlainText(strMessage);
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qInstallMessageHandler(logMessageOutput);

    // 设定父对象
    m_notify = new QtNotification(ui->centralwidget);
    g_msglog_show = ui->plainTextEdit;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    static int s_index = 0;
    s_index++;
    int type = qrand() % 4;
    QString strMsg;
    if (3 == type) {
        strMsg = QString("%1 测试通知消息告警！~").arg(s_index);
        qWarning() << strMsg;
    } else {
        strMsg = QString("%1 测试通知消息！~").arg(s_index);
        qDebug() << strMsg;
    }

    m_notify->setNotification(strMsg, type);
}
